package com.liangzhong.proxy.task
{
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.task.SMD_Task;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.constants.TaskConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.task.TaskProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	import lz.controls.managers.AnimationManager;
	
	/**
	 *任务proxy 
	 * @author lake
	 * 
	 */	
	public class TaskProxy extends BaseProxy
	{
		public static const ProxyName :String = "TaskProxy";
		
		public function TaskProxy(proxyName:String = ProxyName, data:Object=null)
		{
			super(proxyName, data);
		}
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.TASK://公用协议
					
					return onDeal(subCmdID,buffer,dataSize,clientSocket);
					//				case MainCommandType.MAINDEAL:
					//					return onMainDeal(subCmdID,buffer,dataSize,clientSocket);
			}
			return false;
		}
		/**
		 * 处理
		 * 公共协议类型 40
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */
		private function onDeal(subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			var results:int;
			switch(subCmdID)
			{
				case SMD_Task.FINISHTASK:
					getFinishTask(buffer);
					return true;
				case SMD_Task.GETNEWTASK:
					getNewTask(buffer);
					return true;
				case SMD_Task.UPDATATASKSTATUS:
					getTaskProgress(buffer);
					return true;
			}
			return false;	
		}
		/**
		 *请求任务列表 
		 * 
		 */
//		public function reqeusetTaskList():void
//		{
//			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
//			variables.method = HttpRequestConst.SHOW_INFO_LIST;
//			ProxyUtils.instance.webClient(getTaskList,variables,false);
//		}
//		/**
//		 *返回获得任务列表 
//		 * 
//		 */
//		private function getTaskList($data:Object):void
//		{
//			var obj:Object = ProxyUtils.instance.analysisHttpData($data,TaskProxy);
//			if(obj.retcode == 0)
//			{
//				var arr:Array = obj.store;
//				AppData.getInstance().shopDataManager.analsisList(arr);
//			}else
//			{
//			}
//		}
		
		/**
		 *获得新任务 
		 * 
		 */
		private function getNewTask(buffer:ByteArray):void
		{
			var len:int = buffer.readShort();
			for(var i:int = 0;i < len;i++)
			{
				AppData.getInstance().taskManager.getNewTask(buffer.readInt());
			}
		}
		/**
		 *任务完成ID 
		 */
		private var _finishId:int;
		/**
		 *请求完成任务 
		 * 
		 */
		public function requestFinishTask($obj:Object):void
		{
			
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,MainCommandType.TASK,SMD_Task.FINISHTASK);
			var id:int = $obj.id;
			sendBuf.writeInt(id);
			_finishId = id;
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *完成任务 
		 * @param buffer
		 * 
		 */
		private function getFinishTask(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn == 0)
			{
				Alert.showErrorTip("领取成功");
				AppData.getInstance().taskManager.finishTask(_finishId);
				this.sendNotification(TaskProxyConstant.GET_GET_TASK_WARD,{status:true});
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(TaskProxyConstant.GET_GET_TASK_WARD,{status:false});
			}
		}
		
		/**
		 *任务进度 
		 * 
		 */
		private function getTaskProgress(buffer:ByteArray):void
		{
			var len:int = buffer.readShort();
			for(var i:int = 0;i < len;i++)
			{
				AppData.getInstance().taskManager.taskProgress(buffer.readInt(),buffer.readInt(),buffer.readInt());
			}
		}
	}
}